/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：ExperimentConverter.js<br>
 * 日期：2017年3月9日<br>
 * 作者: 王中山<br>
 * 功能：<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.CircuitParser<br>
 * 类型功能：电路解算器<br>
 */
Rainier.UI.V2.ExperimentConverter = Rainier.Basic.CompositeObject.extend({
	
	NAME : "Rainier.UI.V2.ExperimentConverter", 

	/**
	 * 函数名称：Rainier.UI.V2.ExperimentConverter.init<br>
	 * 函数功能：构造函数<br>
	 * 异常信息：无<br>
	 */
    init: function() {
    	this._super();
    },
	
	/**
	 * 函数名称：Rainier.UI.V2.ExperimentConverter.convertExperToXml<br>
	 * 函数功能：将实验对象转化成XML，格式参见《智能指导与自动批改接口标准XML定义.doc》<br>
	 * 参数输入：exper	:实验对象<br>
	 * 参数输出：无<br>
	 * 函数返回：xmlStr	：字符串<br>
	 * 异常信息：无<br>
	 */
	convertExperToXml : function(exper){
		var _this 	= this;
		var xmlStr 	= '';
		if(exper){
			var jsonStr = JSON.stringify(exper,null,4);
			var base64  = window.btoa(unescape(encodeURIComponent(jsonStr)));
			console.log(base64);
			if(exper.experimentScene){
				var intellcorrect 	= '';
				var comLStr			= '';
				var conLStr			= '';
				var componentList = exper.experimentScene.componentList;
				var connectionList = exper.experimentScene.connectionList;

				//应网络要求，如果实验为空，不传输任何数据。
				if (!componentList) {
					return 'exper_empty';
				}

				var connWithNodeList = [];
				$.each(componentList,function(i,component){
					var comStr 			= '';
					var interLStr		= '';
					var propertyLStr	= '';
					var comName			= '';
					var label = component.modelProperty.label;
					$.each(component.portList,function(pI,port){
						var interStr = '';
						interStr 	+= _this.createXmlStringWithoutAttr('name',label+port.modelProperty.name);
						interStr 	+= _this.createXmlStringWithoutAttr('type',port.portType);
						interLStr 	+= _this.createXmlStringWithoutAttr('interface',interStr);
					})
					$.each(component.modelProperty.additional.spiceProperties,function(proI,property){
						var propertyStr = '';
						propertyStr 	+= _this.createXmlStringWithoutAttr('property-name',property.caption);
						propertyStr 	+= _this.createXmlStringWithoutAttr('property-value',property.value.value);
						propertyStr 	+= _this.createXmlStringWithoutAttr('property-value-type','double');
						propertyStr 	+= _this.createXmlStringWithoutAttr('property-valueunit',property.unit.unitType);
						propertyStr 	+= _this.createXmlStringWithoutAttr('property-revisability','Y');
						propertyLStr 	+= _this.createXmlStringWithoutAttr('property',propertyStr);
					})
					comStr 	+= _this.createXmlStringWithoutAttr('component-name',label);
					comStr 	+= _this.createXmlStringWithoutAttr('interface-list',interLStr); 
					comStr 	+= _this.createXmlStringWithoutAttr('property-list',propertyLStr);
					comLStr += _this.createXmlStringWithoutAttr('component',comStr);
				})
				$.each(connectionList,function(i,connect){
					if (connect.componentList && connect.componentList.length > 0) {
						connWithNodeList.push(connect);
					}
				})
				$.each(connectionList,function(i,connect){
					var conStr 				= '';
					var parentLabel 		= '';
					var sourcePortName		= '';
					var targetPortName		= '';
					var parentComponent		= null;
					parentComponent 		= _this.getParentComponent(componentList,connect.modelProperty);
					if(!parentComponent.findS && !parentComponent.findT){
						return true;
					}
					if(!parentComponent.findS || !parentComponent.findT){
						_this.getJoinParent(componentList,connWithNodeList,connect.modelProperty,parentComponent);
					}
					if (parentComponent.findS && parentComponent.findT){
						conStr	+= _this.createXmlStringWithoutAttr('connect-1component-name',parentComponent.sParentLabel);
						conStr	+= _this.createXmlStringWithoutAttr('connect-1interface-name',parentComponent.sParentLabel+parentComponent.sourcePortName);
						conStr	+= _this.createXmlStringWithoutAttr('connect-2component-name',parentComponent.tParentLabel);
						conStr	+= _this.createXmlStringWithoutAttr('connect-2interface-name',parentComponent.tParentLabel+parentComponent.targetPortName);
						conLStr += _this.createXmlStringWithoutAttr('connect',conStr);
					}
				})
				var content = '';
				content += _this.createXmlStringWithoutAttr('exper',base64);
				content += _this.createXmlStringWithoutAttr('component-list',comLStr);
				content += _this.createXmlStringWithoutAttr('connect-list',conLStr);
				if(content){
					intellcorrect = _this.createXmlStringWithoutAttr('intellcorrect',content);
					xmlStr = intellcorrect;
				}
			}
		}
		return xmlStr;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.ExperimentConverter.createXmlStringWithoutAttr<br>
	 * 函数功能：格式化XML<br>
	 * 参数输入：key		： 字符串<br>
	 * 参数输入：value	： 字符串<br>
	 * 参数输出：无<br>
	 * 函数返回：xmlString:xml字符串<br>
	 * 异常信息：无<br>
	 */
	createXmlStringWithoutAttr : function(key,value){
		if(key){
			var xmlString;
			xmlString = '<' +key+ '>';
			xmlString += value;
			xmlString += '</' +key+ '>';
			return xmlString;			
		}
	},

	/**
	 * 函数名称：Rainier.UI.V2.ExperimentConverter.getParentComponent<br>
	 * 函数功能：获取连线两端端口的label及端口名<br>
	 * 参数输入：componentList 	： 元件列表<br>
	 * 参数输入：connectPro		： 连线属性<br>
	 * 参数输出：无<br>
	 * 函数返回：parentObj<br>
	 * 异常信息：无<br>
	 */
	getParentComponent : function(componentList,connectPro){
		if (componentList && componentList.length > 0) {
			var source_id 			= connectPro.source_id;
			var source_parent_id 	= connectPro.source_parent_id;
			var target_id 			= connectPro.target_id;
			var target_parent_id 	= connectPro.target_parent_id;
			var parent = {};				
			$.each(componentList,function(i,component){
				if(source_parent_id === component.uuid || target_parent_id === component.uuid){
					$.each(component.portList,function(j,port){
						if(source_id == port.uuid){
							parent.sParentLabel		= component.modelProperty.label;
							parent.sourcePortName 	= port.modelProperty.name;
							parent.findS			= true;
						}
						else if(target_id == port.uuid){
							parent.tParentLabel		= component.modelProperty.label;
							parent.targetPortName 	= port.modelProperty.name;
							parent.findT			= true;
						}
					})
				}
				if(parent.findS && parent.findT){
					return false;
				}
			})
			return parent;
		}
	},

	/**
	 * 函数名称：Rainier.UI.V2.ExperimentConverter.getJoinParent<br>
	 * 函数功能：获取连线结点的连线的源端口的label及端口名<br>
	 * 参数输入：componentList 		： 元件列表<br>
	 * 参数输入：connWithNodeList 	： 带结点连线列表<br>
	 * 参数输入：connectPro			： 连线属性<br>
	 * 参数输出：parentComponent<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	getJoinParent : function(componentList,connWithNodeList,connectPro,parentComponent){
		if (connWithNodeList && connWithNodeList.length > 0) {
			var parentId 			= null;
			var source_id 			= connectPro.source_id;
			var source_parent_id 	= connectPro.source_parent_id;
			var target_id 			= connectPro.target_id;
			var target_parent_id 	= connectPro.target_parent_id;
			if(!parentComponent.findS){
				parentId 			= source_parent_id;
			}
			else{
				parentId 			= target_parent_id;
			}
			var bFind = false;
			$.each(connWithNodeList,function(i,connect){
				$.each(connect.componentList,function(j,join){
					if (join.uuid == parentId) {
						var source_id 			= connect.modelProperty.source_id;
						var source_parent_id 	= connect.modelProperty.source_parent_id;
						$.each(componentList,function(i,component){
							if(source_parent_id === component.uuid ){
								$.each(component.portList,function(j,port){
									if(source_id == port.uuid){										
										if(!parentComponent.findS){
											parentComponent.sParentLabel		= component.modelProperty.label;
											parentComponent.sourcePortName 	= port.modelProperty.name;
											parentComponent.findS			= true;
										}
										else{
											parentComponent.tParentLabel		= component.modelProperty.label;
											parentComponent.targetPortName 	= port.modelProperty.name;
											parentComponent.findT			= true;
										}
									}
								})
							}
							if(parentComponent.findS && parentComponent.findT){
								return false;
							}
						})
						bFind = true;
						return false;
					}
				})
				return !bFind;
			})
		}
	}
	
});